package com.yiwei.ydd.view;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

public abstract class OnLoadeMoreListener extends RecyclerView.OnScrollListener {

    private int previousTotal = 0; // The total number of items in the dataset after the last load
    private boolean loading = true; // True if we are still waiting for the last set of home to load.
    private int visibleThreshold = 5; // The minimum amount of items to have below your current scroll position before loading more.


    private int currentPage = 1;

//    public boolean enable = true;


//    @Override
//    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
//        super.onScrollStateChanged(recyclerView, newState);
//        if(!loading && !recyclerView.canScrollVertically(1)){
//            loading = true;
//            loadMoreDate();
//        }
//    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        if (isSlideToBottom(recyclerView)) {
            currentPage++;

            onLoadMore(currentPage);
        }
    }

    protected boolean isSlideToBottom(RecyclerView recyclerView) {
        if (recyclerView == null) return false;
        if (recyclerView.computeVerticalScrollExtent() + recyclerView.computeVerticalScrollOffset() >= recyclerView.computeVerticalScrollRange())
            return true;
        return false;
    }

    public void setPage() {
        currentPage = 1;
    }

    public void setPage(int page) {
        currentPage = page;
        this.previousTotal = 0;
        this.loading = false;
//        enable = true;
    }

    public abstract void onLoadMore(int page);
}